import os
import uuid
from PIL import Image
import json
from django.http import response
import codecs
import platform
class UploadFile:
    def upload_file(self,file):
        if(platform.system =="Windows"):
           pa= 'Static\\pic\\'
        else:
           pa= 'Static/pic/'
        name = str(uuid.uuid1())
        name = name.split('-')
        name = "".join(name)
        name = name+'.jpeg'
        pa = pa + name
        res = self.savePic(pa,file)
        thumbnail =self.makeThumbnail(file,pa)

        if(thumbnail==0 or res==0):
            return 0    
        else:
            com = 'http://www.carpenterdormon.org/'
            pic = [{'picture':com+pa,'thumbnail':com+thumbnail}]
            pic = json.dumps(pic)
            print pic
            return pic
    def makeThumbnail(self,file,path):
        SITE_ROOT = os.path.abspath(os.path.dirname(__file__))
        print SITE_ROOT
        root = SITE_ROOT.replace('Cust\\System',path)
        print root
        img = Image.open(root)
        img = img.resize((50,50),Image.ANTIALIAS)
        
        root = root[0:-5]
        root = root+"_small"+'.jpeg'
        path = path[0:-5]
        path = path+"_small"+'.jpeg'
        print root
        try:
           img.save(root)
           return path
        except Exception as e:        
           print e.message
           return 0
        
        
            

    def savePic(self,path,file):
        SITE_ROOT = os.path.abspath(os.path.dirname(__file__))
        print SITE_ROOT
        root = SITE_ROOT.replace('Cust\\System',path)
        print root
        try:
           with open(root, 'wb+') as destination:
             for chunk in file.chunks():           
                 destination.write(chunk)
           return 1
        except Exception as e:
           return 0 
    def downLoadpic(self,path):
        if(platform.system =="Windows"):
               path=path.replace('/','\\') 
       
        
    
        SITE_ROOT = os.path.abspath(os.path.dirname(__file__))
        print SITE_ROOT
        root = SITE_ROOT.replace('Cust\\System',path)
        print root
       
       
        with open(root,'rb') as f:
            while True:
                c = f.read(262144)
                if c:
                    yield c
                else:
                    break
        f.close() 
    def makeHtml(self,content,img):
        
        name = str(uuid.uuid1())
        name = name.split('-')
        name = "".join(name)
        name = name+'.html'
        sysstr = platform.system()    
        pict = '<img src='+img+'>'
        path= 'Static/news/'
        SITE_ROOT = os.path.abspath(os.path.dirname(__file__))     
        root = SITE_ROOT.replace('Cust\\System','Static\\news\\')
        header = root+'header.html'
        footer = root+'footer.html'
        path = path + name
        rootpa = root + name
        content = content.encode("utf-8")
        try:
          
          open(rootpa,'wb+').write(open(header,'rb').read())
         # open(rootpa,'a').write(pict)
          open(rootpa,'a').write(content)
          open(rootpa,'a').write(open(footer,'rb').read())
         
          return path
        except Exception as e:
          return e.message
           
             
        
        
